﻿using APIShoppingSystem.GetSession;
using APIShoppingSystem.Model;
using APIShoppingSystem.Models.UserInfoViewModel;
using IShoppingBll;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RepositoryCommon;
using ShoppingEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using static ShoppingEntity.UserInfo;

namespace APIShoppingSystem.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserInfoController : ControllerBase
    {

        private IUserInfoBll _userInfoBll;
        private ITop_upBll _top_UpBll;
        public UserInfoController(IUserInfoBll userInfoBll, ITop_upBll top_UpBll)
        {
            _userInfoBll = userInfoBll;
            _top_UpBll = top_UpBll;
        }

        /// <summary>
        /// 注册用户信息：即添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddRegisteredUser([FromForm] AddUserInfoViewModel model)
        {
            CustomActionResult res = new CustomActionResult();
            if (string.IsNullOrEmpty(model.Account))
            {
                res.Msg = "用户账号不能为空";
                return new JsonResult(res);
            }
            else if (string.IsNullOrEmpty(model.PassWord))
            {
                res.Msg = "密码不能为空";
                return new JsonResult(res);
            }
            else if (string.IsNullOrEmpty(model.UserName))
            {
                res.Msg = "用户名不能为空";
                return new JsonResult(res);
            }
            //为密码Md5加密
            string PwdMD5 = MD5Helper.MD5Encrypt64(model.PassWord);
            UserInfo entity = new UserInfo()
            {
                ID = Guid.NewGuid().ToString(),
                Account = model.Account,
                UserName = model.UserName,
                PhoneNum = model.PhoneNum,
                Email = model.Email,
                Sex = model.Sex,
                Address = model.Address,
                PassWord = PwdMD5,
                CreateTime = DateTime.Now,
                IsAdmin = false,
                UserInfoRole = model.UserInfoRole
            };
            bool isSuccess = _userInfoBll.AddUserInfo(entity);

            if (isSuccess)
            {
                res.IsSuccess = true;
                res.Status = 1;
                res.Msg = "添加成功";
                return new JsonResult(res);
            }
            else
            {
                return new JsonResult(res);
            }
        }

        /// <summary>
        /// 获取用户更新页面信息
        /// </summary>
        /// <param name="userInfoId"></param>
        /// <returns></returns>
       [HttpGet]
        public IActionResult GetUpdateUserInfo(string userInfoId)
        {
            CustomActionResult res = new CustomActionResult();
            //获取用户信息
            UserInfo userInfo = _userInfoBll.GetEntity(userInfoId);
            res.IsSuccess = true;
            res.Status = 1;
            res.Msg = "成功";
            res.Datas = new
            {
                userInfo = userInfo,

            };
            return new JsonResult(res);

        }

        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult UpdateUserInfo([FromForm]UpdateUserInfoViewModel model)
        {
            CustomActionResult res = new CustomActionResult();
            if (_userInfoBll.UpdateUserInfo(model.ID, model.UserName, model.Sex, model.PhoneNum, model.Address))
            {
                res.IsSuccess = true;
                res.Status = 1;
                res.Msg = "修改用户信息成功";
                return new JsonResult(res);
            }
            else
            {
                return new JsonResult(res);
            }
        }

        /// <summary>
        /// 注销用户信息
        /// </summary>
        /// <param name="userInfoId"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult CancellUserInfo([FromForm] string userInfoId)
        {
            CustomActionResult res = new CustomActionResult();
            if (_userInfoBll.SoftDeleteEntity(userInfoId))
            {
                res.Status = 1;
                res.Msg = "成功";
                res.IsSuccess = true;
                return new JsonResult(res);
            }
            else
            {
                return new JsonResult(res);
            }
        }

        /// <summary>
        /// 用户信息页面展示
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="userName"></param>
        /// <param name="PhoneNum"></param>
        /// <returns></returns>
       [HttpGet]
        public IActionResult GetUserInfoList(int page, int limit, string userName, string PhoneNum)
        {
            int count;

            object userInfos = _userInfoBll.GetUserInfoListByPage(page, limit, out count, userName, PhoneNum);

            return new JsonResult(new
            {
                code = 0,
                msg = "成功",
                count = count,
                data = userInfos
            });
        }

        /// <summary>
        /// 筛选用户的角色
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult IsUserRole()
        {
            CustomActionResult res = new CustomActionResult();
            UserInfo userInfo = this.GetSessionUserInfo();
            var userInfo_RoleInfo = _userInfoBll.IsUserRole(userInfo.ID);
            res.Msg = "成功";
            res.Status = 1;
            res.IsSuccess = true;
            res.Datas = userInfo_RoleInfo;
            return new JsonResult(res);
        }

        /// <summary>
        /// 充值金额方法
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddTop_upMethod([FromForm] AddTop_upViewModel model)
        {
            CustomActionResult res = new CustomActionResult();
            UserInfo user = this.GetSessionUserInfo();
            Top_up entity = new Top_up()
            {
                ID = Guid.NewGuid().ToString(),
                Amount = model.Amount,
                Way = model.Way,
                CreateTime = DateTime.Now,
                IsDelete = false,
                UserId = user.ID
            };
            bool isSuccess = _top_UpBll.AddTop_Up(entity);

            if (isSuccess)
            {
                res.IsSuccess = true;
                res.Status = 1;
                res.Msg = "充值成功";
                return new JsonResult(res);
            }
            else
            {
                return new JsonResult(res);
            }
        }


        /// <summary>
        /// 待审批用户方法
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ApproverUserInfoListByPage(int page, int limit, string userName, UserInfoRoleInfoEnum UserInfoRole)
        {
            int count;

            object userInfos = _userInfoBll.GetApproverUserInfoListByPage(page, limit, out count, userName, UserInfoRole);

            return new JsonResult(new
            {
                code = 0,
                msg = "成功",
                count = count,
                data = userInfos
            });
        }


        /// <summary>
        /// 同意审批
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult ConsentApproverUserRolen([FromForm] string userInfoId)
        {
            CustomActionResult res = new CustomActionResult();

            bool isSuccess = _userInfoBll.ConsentApproverUserInfo(userInfoId);

            if (isSuccess)
            {
                res.IsSuccess = true;
                res.Status = 1;
                res.Msg = "审批通过";
            }
            return new JsonResult(res);


        }


        /// <summary>
        /// 驳回审批
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns> 
        [HttpPost]
        public IActionResult TurnApproverUserRolen([FromForm] string userInfoId)
        {
            CustomActionResult res = new CustomActionResult();

            bool isSuccess = _userInfoBll.TurnApproverUserInfo(userInfoId);

            if (isSuccess)
            {
                res.IsSuccess = true;
                res.Status = 1;
                res.Msg = "驳回成功";
            }
            return new JsonResult(res);


        }
    }
}
